package
{
	import anshe.event.TitleEvent;
	import anshe.object.TitleObject;
	import anshe.struct.game.GUserInfo;
	import anshe.widget.AnsheClient;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import imvu.rbox.struct.CGamePlayerInfo;

	public class AnsheChat extends AnsheClient
	{
		public var mcTitle:TitleObject;		//	右上角标题
		public var txtMyName:TextField;		//	显示自己的名称
		public var txtOnline:TextField;		//	显示在线玩家
		public var txtMessage:TextField;	//	聊天信息
		public var btnSend:SimpleButton;	//	消息发送按钮
		public var txtInput:TextField;		//	信息输入框
		
		private var infoArray:Array;		//	消息列表
		private var maxShow:Number;			//	最多显示消息的条目数
		private var myInfo:GUserInfo;		//	当前玩家的信息
		
		private var sprMain:Sprite;			//	主对象界面
		
		public function AnsheChat()
		{
			super("newAcsChat", "AnsheChat");

			sprMain = new Sprite();
			
			var tArray:Array = new Array();
			for(var i:int = 0; i < this.numChildren; i++)
			{
				this.sprMain.addChild(this.getChildAt(i));	
			}
			
			this.sprMain.addChild(txtInput);
			this.sprMain.addChild(txtMyName);
			this.sprMain.addChild(txtOnline);
			
			this.addChild(sprMain);
			this.addChild(mcTitle);			
			
			this.txtMyName.text = "";
			this.txtOnline.text = "";
			this.infoArray = new Array();
			this.maxShow = 150;
			
			this.txtOnline.mouseEnabled = false;
			
			this.btnSend.addEventListener(MouseEvent.CLICK, onSendMessage);		
				
			//	设置标题对象事件侦听
			this.mcTitle.addEventListener(TitleEvent.MIN_WINDOW, onWinMinClick);
			this.mcTitle.addEventListener(TitleEvent.RETURN_WINDOW, onWinReturnClick);
			this.mcTitle.addEventListener(TitleEvent.CLOSE_WINDOW, onWinCloseClick);	
			this.mcTitle.addEventListener(TitleEvent.SHOW_HELP, onShowHelpClick);
			
			this.mcTitle.addEventListener(TitleEvent.START_DRAG, onStageStartDrag);
			this.mcTitle.addEventListener(TitleEvent.STOP_DRAG, onStageStopDrag);
		}
		
		/** 点击 最小化 按钮后功能 **/
		private function onWinMinClick(e:Event):void
		{
			this.mcTitle.btnMin.visible = false;
			this.mcTitle.btnReturn.visible = true;
			
			this.sprMain.visible = false;
		}

		/** 点击 还原窗口 按钮后功能 **/
		private function onWinReturnClick(e:Event):void
		{
			this.sprMain.visible = true;
		}

		/** 点击 帮助 按钮后触发事件 */
		private function onShowHelpClick(e:Event):void
		{
			// flash.net.navigateToURL(new URLRequest("http://system.anshechung.com/help/magicpainter"), "_blank");
		}	

		/** 点击 关闭 按钮 后功能  （上级对象删除自己） **/
		private function onWinCloseClick(e:Event):void
		{
			RemoveThisGame();
		}
		
		/** 开始拖动整体对象（整个 MC 本身） **/
		private function onStageStartDrag(e:*):void
		{
			this.startDrag();
			
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onStageStopDrag);
		}
		
		/** 停止拖动对象（整个 MC 本身）**/
		private function onStageStopDrag(e:*):void
		{
			this.stopDrag();

			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onStageStopDrag);
		}
		
		public override function InitGameListener():void
		{
			super.InitGameListener();
			
			trace("注册侦听");
			AddListener("onNewMessage", onNewMessage);
		}
		
		protected override function InitGame():void
		{
			super.InitGame();
						
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownEvent);

			
			// JoinGame();
			
			if(this.space)
			{
				//**
				if(this.space.myInfo)
				{
					myInfo = new CGamePlayerInfo();					
					myInfo.name = this.space.myInfo.userName;
					myInfo.userID = this.space.myInfo.userID;
					myInfo.userFace = this.space.myInfo.userFace;
					myInfo.isGirl = this.space.myInfo.isGirl;
					
					this.txtMyName.text = "MyName: " + myInfo.userNick;
					
					ShowMessage("查到当前用户的相关信息:: " + myInfo);
				}
				else
				{
					ShowMessage("找不到用户信息");
				}
			}
			else
			{
				ShowMessage("找不到 space 对象：：" + this.space);
			}
		}
		
		public function onNewMessage(...argc):void
		{
			trace("收到消息::", argc);
			this.infoArray.unshift(String(argc[0] + " : " + argc[1]));
			
			RushInfoShow();
		}
		
		private function onSendMessage(e:MouseEvent):void
		{
			var sendStr:String = this.txtInput.text.slice();
			sendStr.replace(" ","");
			sendStr.replace("\n","");
			
			if(sendStr.length > 1)
			{
				SendCommand("sendMessage", "onSendMsgError", 0, sendStr);
				
				this.txtInput.text = "";
				
				trace("traceString::" + sendStr);
			}
		}
		
		private function onKeyDownEvent(e:KeyboardEvent):void
		{
			if(e.keyCode == 13)
			{
				onSendMessage(null);
			}
		}
		
		public function onSendMsgError(argc:Array):void
		{
			this.infoArray.unshift("[Send Message Error!!]");
			
			RushInfoShow();
		}
		
		private function RushInfoShow():void
		{
			var dep:Number = (this.infoArray.length > this.maxShow) ? maxShow : this.infoArray.length;
			this.txtMessage.text = "";
			
			var redFormat:TextFormat = new TextFormat();
			redFormat.color = 0xFF0000;
			redFormat.bold = true;
			
			for(var i:int = dep - 1; i >= 0; i--)
			{				
				this.txtMessage.appendText(this.infoArray[i]);	
				
				var bx:Number = this.txtMessage.text.lastIndexOf(":");
				var by:Number = this.txtMessage.text.lastIndexOf(String.fromCharCode(13));
								
				if(by < 0 && by < bx)
				{
					this.txtMessage.setTextFormat(redFormat, 1, bx);
				}
				else
				{				
					this.txtMessage.setTextFormat(redFormat, by, bx);
				}				
				this.txtMessage.appendText("\n");			
			}
			
			this.txtMessage.setTextFormat(redFormat, 0, 2);
			
			this.txtMessage.scrollV = this.txtMessage.maxScrollV;			
		}
	}
}